package com.zsgeek.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.mysql.jdbc.StringUtils;
import com.zsgeek.dao.UserDao;
import com.zsgeek.entity.User;
import com.zsgeek.util.StringHelper;

public class UserServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String m = req.getParameter("m");
		if (m == null) {
			String email = req.getParameter("email");
			String password = req.getParameter("password");
			System.err.println("email======="+email);
			System.err.println("password======="+password);
			if (!StringUtils.isNullOrEmpty(email) && !StringUtils.isNullOrEmpty(password)) {
				User user = UserDao.getUserByUserEmail(email);
				System.out.println("登录结果是：" + user);
				if(user != null){
					if (user.getPassword().equals(StringHelper.MD5(password))) {
						HttpSession session = req.getSession();
						session.setAttribute("user", email);
						session.setMaxInactiveInterval(3600);
						resp.sendRedirect(req.getContextPath() + "/main.jsp");
						return;
					} else {
						req.setAttribute("msg", "密码错误！");
					}
				} else {
					req.setAttribute("msg", "帐号错误！");
				}
			} else {
				req.setAttribute("msg", "帐号或者密码不能为空！");
			}
			req.getRequestDispatcher("login.jsp").forward(req, resp);
			
		} else if ("main".equals(m)) {
			// 跳转主页
			main(req, resp);
		} else if ("reg".equals(m)) {
			// 用户注册
			reg(req, resp);
		} else if ("info".equals(m)) {
			// 查询用户信息
			info(req, resp);
		} else if ("update".equals(m)) {
			// 修改用户信息
			update(req, resp);
		} else if ("reset".equals(m)){
			//忘记密码
			reset(req, resp);
		}
	}

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	/**
	 * 忘记密码
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void reset(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//HttpSession session = req.getSession();
		String email = req.getParameter("email");
		String password = req.getParameter("password");
		User user = UserDao.getUserByUserEmail(email);
		System.out.println("user=" + user);
		boolean b = false;
		if (user != null) {
			if (!StringUtils.isNullOrEmpty(password)) {
				user.setPassword(StringHelper.MD5(password));
				b = UserDao.update(user);
			}
		}
		if (b) {
			req.setAttribute("msg", "恭喜您，修改成功！<a href='login.jsp'>重新登录</a>");
		} else {
			req.setAttribute("msg", "抱歉修改失败！！");
		}
		req.getRequestDispatcher("reset-password.jsp").forward(req, resp);
	}


	/**
	 * 跳转主页
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void main(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.getRequestDispatcher("main.jsp").forward(req, resp);
	}
	
	protected void reg(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String userName = req.getParameter("userName");
		String password = req.getParameter("password");
		String email = req.getParameter("email");
		User user = UserDao.getUserByUserEmail(email);
		if (user == null) {
			if (!StringUtils.isNullOrEmpty(password)) {
				// 表示可以使用
				user = new User();
				user.setUserName(userName);
				user.setPassword(StringHelper.MD5(password));
				user.setEmail(email);
				UserDao.register(user);
				req.setAttribute("msg", "注册成功！<a href='login.jsp'>立即登录</a>");
				
			} else {
				req.setAttribute("msg", "注册失败！");
			}
		} else {
			// 表示不可使用
			if (!StringUtils.isNullOrEmpty(password)) {
				req.setAttribute("msg", "邮箱已经被注册了，请更换！");
			} else {
				req.setAttribute("msg", "注册失败！");
			}
		}
		req.getRequestDispatcher("register.jsp").forward(req, resp);
		
	}
	
	/**
	 * 查询用户信息
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void info(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session = req.getSession();
		
		User user = UserDao.getUserByUserEmail(session.getAttribute("user").toString());
		System.out.println("user=" + user);
		req.setAttribute("user", user);
		req.getRequestDispatcher("reset.jsp").forward(req, resp);

	}

	protected void update(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("进入update了。。。。");
		HttpSession session = req.getSession();
		String email = session.getAttribute("user").toString();
		String userName = req.getParameter("userName");
		String password = req.getParameter("password");
		User user = UserDao.getUserByUserEmail(email);
		user.setUserName(userName);
		user.setPassword(StringHelper.MD5(password));
		boolean b = false;
		b = UserDao.update(user);
		if (b) {
			req.setAttribute("msg", "恭喜您，修改成功！<a href='login.jsp' target='_parent'>重新登录</a>");
		} else {
			req.setAttribute("msg", "抱歉修改失败！！");
		}
		UserDao.getUserByUserEmail(email);
		req.setAttribute("user", user);
		req.getRequestDispatcher("reset.jsp").forward(req, resp);
	}

}
